#include "GreenDot.h"
#include "Screens\GameScreen.h"

#include "Isle.h"
#include "Objects/Particle.h"


GreenDot::GreenDot(GameState *gameState):
				PhysicalObject(gameState)
{
	addProperty("GreenDot");
	setLayer(3);

	m_texture = gameState->getTextureManager().getTexture("greendot.png");

	setSize(b2Vec2(0.5, 0.5f));

	getBodyDef()->type = b2_dynamicBody;
	getBodyDef()->fixedRotation = false;
	getBodyDef()->gravityScale = 0;
	b2FixtureDef *fixDef = new b2FixtureDef;
	b2CircleShape *shape = new b2CircleShape;
	shape->m_radius = 1.0f;
	fixDef->shape = shape;
	fixDef->density = 2.0f;
	fixDef->isSensor = true;
	addFixtureDef(fixDef);
	setRotation(0.0f);

	m_time = 0;
}


GreenDot::~GreenDot()
{
}


void GreenDot::update(double dt){
	m_actualSize = m_size; 
	m_actualSize *= ((float)sin(m_time*5)+3.0f);

	if(!isAlive()){
		for(int i = -2; i < 3; i++){
			for(int j = -2; j < 3; j++){
				b2Vec2 particlePos = b2Vec2(getPosition().x+i*0.05f, getPosition().y+j*0.1f);
				b2Vec2 posSum = b2Vec2((((rand()%10)/10.0f)-0.5f)*0.1f, (((rand()%10)/10.0f)-0.5f)*0.5f);
				posSum *= 4.0f;
				particlePos += posSum;
				
				Particle *particle = m_gameState->getCurrentScreen()->createParticle();
				particle->setShader(m_gameState->getShaderManager().getShader("iceParticle"));
				if(rand()%2){
					particle->setColor(0.4f, 0.8f, 0.2f, 0.7f);
				}else{
					particle->setColor(0.4f, 0.6f, 0.2f, 0.7f);
				}
				particle->setEndColor(0.8f, 0.8f, 0.8f, 0.5f);
				particle->setSize(b2Vec2(3.0f, 3.0f));
				particle->setPosition(particlePos);
				particle->setLifeTime(0.2f);
				particle->setVel(b2Vec2(rand()%30-15.0f, rand()%30-15.0f));
				//, 3);
			}
		}
		GameScreen *gScreen = static_cast<GameScreen*>(m_screen);
		gScreen->addDot();
		m_gameState->getSoundManager().playSound("dot.wav");
	}

	m_time += dt;
}


void GreenDot::draw(){

	//drawFixtures();

	b2Vec2 pos = getPosition();
	
	glPushMatrix();
	{
		
		glTranslatef(pos.x, pos.y, 0); 
		glScalef(m_actualSize.x*2, m_actualSize.y*2, 1);
		glRotatef(m_body->GetAngle()*57.2957795f, 0, 0, 1);

		sf::Texture::bind(m_texture); 

		glBegin(GL_QUADS);
		{
			glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.5f, -0.5f, -0.2f);
			glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, -0.5f, -0.2f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, 0.5f, -0.2f);
			glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.5f, 0.5f, -0.2f);
		}
		glEnd();
	}
	glPopMatrix();
	
	sf::Texture::bind(NULL);
}



void GreenDot::beginContact(b2Contact *contact){
	b2Fixture *myFixture;
	PhysicalObject *me;
	PhysicalObject *other;

	getContactEntities(contact, &me, &other, &myFixture);

	if(other->hasProperty("Fire") || other->hasProperty("Player")){
		this->kill();
	}
}


void GreenDot::endContact(b2Contact *contact){
	b2Fixture *myFixture;
	PhysicalObject *me;
	PhysicalObject *other;
	getContactEntities(contact, &me, &other, &myFixture);

	if(other->hasProperty("Fire") || other->hasProperty("Player")){
		this->kill();
	}

}